<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;

class System
{
    // 基本信息
    public function systemInfo()
    {
        if (request()->isAjax()) {
            $params = request()->only(['id', 'web_name', 'copyright', 'logo', 'about_us', 'android_version',
                'android_url', 'ios_version', 'ios_url', 'update_desc', 'up_ios', 'service_phone']);
            $validate = (new \app\validate\System())->scene('edit');
            if (!$validate->check($params)) {
                result(null, 0, $validate->getError(), 'json');
            }
            Db::startTrans();
            try {
                $updateDataSystem = [];
                foreach ($params as $k => $v) {
                    if ($v) {
                        $updateDataSystem[$k] = $v;
                    }
                }
                \app\model\System::update($updateDataSystem);
                Db::commit();
            } catch (\Throwable $e) {
                Db::rollback();
                result(null, $e->getCode(), $e->getMessage());
            }
            result(null, 1, '设置成功');
        }
        $systemInfo = \app\model\System::findOrEmpty(1);
        View::assign('systemInfo', $systemInfo);
        return view();
    }

    // APP信息
    public function appInfo()
    {
        $systemInfo = \app\model\System::findOrEmpty(1);
        View::assign('systemInfo', $systemInfo);
        return view();
    }
}
